package com.hi.hiparty.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hi.hiparty.R;
import com.hi.hiparty.util.ApiHelper;
import com.hi.hiparty.util.HttpUtil;
import com.hi.hiparty.util.StringUtils;

public class RegisterActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		Button nextButton = (Button) findViewById(R.id.btn_reg_next);
		nextButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				EditText loginnameTextInput = (EditText) findViewById(R.id.edit_text_loginname);
				EditText passwordTextInput = (EditText) findViewById(R.id.edit_text_loginpwd);
				EditText confirmPwdTextInput = (EditText) findViewById(R.id.edit_text_confirmLoginpwd);

				String name = loginnameTextInput.getText().toString();
				String pwd = passwordTextInput.getText().toString();
				String confPwd = confirmPwdTextInput.getText().toString();

				if (null == name || name.trim().equals("")) {
					showErrorMessageToast("登陆名不能为空");
					return;
				}

				if (!name.matches("^[a-zA-Z0-9_]*")) {
					showErrorMessageToast("登陆名只能包含字母和数字");
					return;
				}

				if (!pwd.equals(confPwd)) {
					showErrorMessageToast("两次输入的密码不符");
					return;
				}

				if (pwd.length() < 6) {
					showErrorMessageToast("密码长度要超过6位数");
					return;
				}

				RegAsyncTask regAsyncTask = new RegAsyncTask();
				regAsyncTask.execute(name, pwd);
			}

		});
	}

	public class RegAsyncTask extends AsyncTask<String, Void, Boolean> {

		private ProgressDialog dialog;

		private String errorMessage;

		private String userid;

		protected void onPreExecute() {
			dialog = ProgressDialog.show(RegisterActivity.this, "",
					"正在发送服务器验证信息...", true);
		}

		protected void onPostExecute(final Boolean success) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (success) {
				Intent intent = new Intent(RegisterActivity.this,
						RegisterFillNicknameActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userid", userid);
				intent.putExtras(bundle);
				startActivity(intent);
			} else {
				Toast.makeText(RegisterActivity.this, errorMessage,
						Toast.LENGTH_LONG).show();
			}
		}

		protected Boolean doInBackground(final String... args) {
			if (null == args || args.length < 2) {
				return false;
			}
			String name = args[0];
			String pwd = args[1];
			String url = ApiHelper.API_REGISTER + name + "/" + pwd;
			String jsonString = HttpUtil.GET(url);
			if (StringUtils.isBlank(jsonString)) {
				errorMessage = "您的网络信号不太好";
				return false;
			}
			try {
				JSONObject json = new JSONObject(jsonString);
				if (json.getInt("success") == 1) {
					// 进入登陆后主页
					userid = json.getString("userid");
					return true;
				} else {
					errorMessage = json.getString("error");
					return false;
				}
			} catch (JSONException e) {
				errorMessage = "服务请求格式异常，请稍后再试";
				return false;
			}
		}
	}

	private void showErrorMessageToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
}
